/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-03-17
*/
package com.rzico.basics.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Brand;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.service.BrandService;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 品牌档案控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-03-17
 */
@Api(description = "品牌档案接口")
@RestController
@RequestMapping("/brand")
public class BrandController extends BaseController {

    @Autowired
    private BrandService brandService;

    @Autowired
    private EnterpriseService enterpriseService;

    /**
     * 分页查询品牌档案
     *
     * @return
    */
    @ApiOperation("分页查询品牌档案")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "mchId", value = "商户id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "tagIds", value = "标签",   dataType = "String", paramType = "query")
    })
    public CommResult<Brand> list(String mchId,Long [] tagIds, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sortField", "orders");
        params.put("sortType","ASC");

        if (StringUtils.isNotEmpty(mchId)){
            params.put("mchId", mchId);
        }

        if (tagIds!=null && tagIds.length>0) {
            params.put("tagIds",tagIds);
        }

        Enterprise enterprise = enterpriseService.findByMch(mchId);
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Brand> list = brandService.selectList(params);
        PageResult<Brand> pageResult = new PageResult<Brand>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }


}
